package com.jbh.netlibrary.v;

import static com.jbh.netlibrary.m.NetManager.TOAST;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.jbh.netlibrary.R;
import com.jbh.netlibrary.i.Callback;
import com.jbh.netlibrary.i.NetStateListener;
import com.jbh.netlibrary.m.NetManager;
import com.jbh.netlibrary.u.DeviceNetUtil;
import com.jbh.netlibrary.u.ForActivityUtil;


public class NetErrorView extends LinearLayout {

    private TextView btn_refresh, tv_net_error_msg;
    private ImageView im_error;

    public NetErrorView(Context context) {
        this(context, null);
    }

    public NetErrorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NetErrorView(final Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDefault(context);
    }

    private void initDefault(final Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.net_error_layout, this, false);
        addView(view);
        btn_refresh = view.findViewById(R.id.btn_refresh);
        im_error = view.findViewById(R.id.im_error);
        tv_net_error_msg = view.findViewById(R.id.tv_net_error_msg);
        view.findViewById(R.id.btn_open_net).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ForActivityUtil.toSettingActivity(context);
            }
        });
        view.findViewById(R.id.btn_refresh).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DeviceNetUtil.checkConnected(context, new Callback<Integer>() {
                    @Override
                    public void onState(Integer integer) {
                        if (integer == 0) {
                            dismiss();
                            if (NetManager.getInstance().config != null
                                    && NetManager.getInstance().config.getNetStateListener() != null)
                                NetManager.getInstance().config.getNetStateListener().onRefresh();
                        } else {
                            TOAST(context, "网络已断开，请检查！");
                        }
                    }
                });
            }
        });
    }


    /**
     * 設置错误占位图
     *
     * @param iconError
     */
    public void setIm_error(int iconError) {
        im_error.setImageResource(iconError);
    }


    /**
     * @param msg
     */
    public void setErrorMsg(String msg) {
        tv_net_error_msg.setText(msg);
        if (!TextUtils.isEmpty(msg)) {
            tv_net_error_msg.setVisibility(VISIBLE);
        } else {
            tv_net_error_msg.setVisibility(GONE);
        }
    }


    /**
     * @param onRefreshListener
     */
    public void setOnRefreshListener(View.OnClickListener onRefreshListener) {
        btn_refresh.setOnClickListener(onRefreshListener);
    }


    /**
     * 显示
     *
     * @param activity
     */
    private static FrameLayout mDecorView;

    public static void show(Activity activity) {
        dismiss();
        if (activity != null && !activity.isFinishing()) {
            //添加定制的布局
            addCustomView(activity);
            //将需要显示的view添加到容器中
            mDecorView = (FrameLayout) activity.getWindow().getDecorView();
            mDecorView.addView(errorView);
        }
    }


    /**
     * 显示view的
     *
     * @param activity
     */
    public static NetErrorView errorView;

    private static void addCustomView(Activity activity) {
        if (errorView == null) {
            errorView = new NetErrorView(activity);
        }
        if (NetManager.getInstance().config != null && NetManager.getInstance().config.getNoNetView() != null) {
            errorView.removeAllViews();
            errorView.addView(NetManager.getInstance().config.getNoNetView(), new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
    }


    public static void dismiss() {
        if (mDecorView == null) return;
        mDecorView.removeView(errorView);
    }


    /**
     * 释放
     */
    public static void onDestroy() {
        mDecorView = null;
        errorView = null;
    }
}
